package com.wb.core08_常用类.obj;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class Test01 {
    public static void main(String[] args) {
//        getClassTest();
        equalsTest();
    }

    public static void getClassTest(){
        Person person = new Man();
        Person person2 = new WoMan();
        System.out.println(person);
        // getClass():用于返回当前对象的运行时类型
        // 对于person,返回值即为Man的class类型
        System.out.println(person.getClass());
        System.out.println(person2.getClass());

        if(person instanceof Man){
            Man man = (Man) person;
            man.work();
        }

        // 每一个类也可以直接查看class属性,其返回的也是当前类的class类型
        // 语法:类名.class
        System.out.println(Person.class);
        System.out.println(Man.class);
        System.out.println(WoMan.class);

        // 我们可以通过当前对象的class类型来判断该对象是否可以转换成指定类型
        if(person2.getClass() == WoMan.class){
            WoMan woMan = (WoMan) person2;
            woMan.shopping();
        }
    }

    public static void equalsTest(){
        Person person = new Person("admin",18);
        Person person2 = new Person("admin",18);
        System.out.println(person == person2);  // false
        System.out.println(person.equals(person2)); // false
        String s1 = new String("admin");
        String s2 = new String("admin");
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }

}
